From 763ba535eb2fe8b92ab5fd9584c77f2fe22b8e42 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 25 May 2016 17:06:25 -0700 Subject: [PATCH] Shared the test suite into multiple binaries Compiling everything in one binary was getting annoying as it just took forever to build, instead shard it all up so we can build just particular test suites at a time. --- Cargo.lock | 22 +++ Cargo.toml | 7 +- tests/{test_bad_config.rs => bad-config.rs} | 7 +- ..._manifest_path.rs => bad-manifest-path.rs} | 5 +- tests/{test_cargo_bench.rs => bench.rs} | 55 +++--- ...test_cargo_build_auth.rs => build-auth.rs} | 11 +- .../{test_cargo_build_lib.rs => build-lib.rs} | 6 +- ...ompile_custom_build.rs => build-script.rs} | 52 +++--- tests/{test_cargo_compile.rs => build.rs} | 28 +-- tests/{test_cargo.rs => cargo.rs} | 13 +- tests/cargotest/Cargo.toml | 24 +++ tests/cargotest/install.rs | 30 ++++ tests/cargotest/lib.rs | 62 +++++++ tests/{ => cargotest}/support/git.rs | 86 ++++----- tests/{ => cargotest}/support/mod.rs | 79 +++------ tests/cargotest/support/paths.rs | 146 +++++++++++++++ tests/{ => cargotest}/support/registry.rs | 47 +++-- tests/{test_cargo_cfg.rs => cfg.rs} | 22 ++- tests/{test_cargo_clean.rs => clean.rs} | 7 +- ...test_cargo_concurrent.rs => concurrent.rs} | 19 +- tests/{test_cargo_config.rs => config.rs} | 5 +- ...argo_cross_compile.rs => cross-compile.rs} | 23 ++- tests/{test_cargo_death.rs => death.rs} | 7 +- tests/{test_cargo_doc.rs => doc.rs} | 10 +- tests/{test_cargo_features.rs => features.rs} | 12 +- tests/{test_cargo_fetch.rs => fetch.rs} | 5 +- .../{test_cargo_freshness.rs => freshness.rs} | 20 ++- ...erate_lockfile.rs => generate-lockfile.rs} | 5 +- ...{test_cargo_compile_git_deps.rs => git.rs} | 31 ++-- tests/{test_cargo_init.rs => init.rs} | 11 +- tests/{test_cargo_install.rs => install.rs} | 50 ++---- tests/{test_cargo_metadata.rs => metadata.rs} | 8 +- ...test_cargo_net_config.rs => net-config.rs} | 5 +- tests/{test_cargo_new.rs => new.rs} | 36 ++-- .../{test_cargo_overrides.rs => overrides.rs} | 12 +- tests/{test_cargo_package.rs => package.rs} | 21 ++- ...est_cargo_compile_path_deps.rs => path.rs} | 21 ++- ...st_cargo_compile_plugins.rs => plugins.rs} | 12 +- tests/{test_cargo_profiles.rs => profiles.rs} | 5 +- tests/{test_cargo_publish.rs => publish.rs} | 16 +- ...argo_read_manifest.rs => read-manifest.rs} | 5 +- tests/{test_cargo_registry.rs => registry.rs} | 32 ++-- tests/{test_cargo_run.rs => run.rs} | 5 +- tests/{test_cargo_rustc.rs => rustc.rs} | 6 +- tests/{test_cargo_rustdoc.rs => rustdoc.rs} | 6 +- ...argo_compile_rustflags.rs => rustflags.rs} | 27 +-- tests/{test_cargo_search.rs => search.rs} | 17 +- tests/{test_shell.rs => shell.rs} | 16 +- tests/support/paths.rs | 166 ------------------ tests/{test_cargo_test.rs => test.rs} | 17 +- tests/tests.rs | 105 ----------- ...test_cargo_tool_paths.rs => tool-paths.rs} | 12 +- ...go_verify_project.rs => verify-project.rs} | 5 +- tests/{test_cargo_version.rs => version.rs} | 8 +- 54 files changed, 793 insertions(+), 677 deletions(-) rename tests/{test_bad_config.rs => bad-config.rs} (98%) rename tests/{test_bad_manifest_path.rs => bad-manifest-path.rs} (98%) rename tests/{test_cargo_bench.rs => bench.rs} (96%) rename tests/{test_cargo_build_auth.rs => build-auth.rs} (97%) rename tests/{test_cargo_build_lib.rs => build-lib.rs} (94%) rename tests/{test_cargo_compile_custom_build.rs => build-script.rs} (98%) rename tests/{test_cargo_compile.rs => build.rs} (99%) rename tests/{test_cargo.rs => cargo.rs} (95%) create mode 100644 tests/cargotest/Cargo.toml create mode 100644 tests/cargotest/install.rs create mode 100644 tests/cargotest/lib.rs rename tests/{ => cargotest}/support/git.rs (52%) rename tests/{ => cargotest}/support/mod.rs (90%) create mode 100644 tests/cargotest/support/paths.rs rename tests/{ => cargotest}/support/registry.rs (83%) rename tests/{test_cargo_cfg.rs => cfg.rs} (96%) rename tests/{test_cargo_clean.rs => clean.rs} (97%) rename tests/{test_cargo_concurrent.rs => concurrent.rs} (96%) rename tests/{test_cargo_config.rs => config.rs} (86%) rename tests/{test_cargo_cross_compile.rs => cross-compile.rs} (98%) rename tests/{test_cargo_death.rs => death.rs} (96%) rename tests/{test_cargo_doc.rs => doc.rs} (98%) rename tests/{test_cargo_features.rs => features.rs} (98%) rename tests/{test_cargo_fetch.rs => fetch.rs} (82%) rename tests/{test_cargo_freshness.rs => freshness.rs} (96%) rename tests/{test_cargo_generate_lockfile.rs => generate-lockfile.rs} (98%) rename tests/{test_cargo_compile_git_deps.rs => git.rs} (98%) rename tests/{test_cargo_init.rs => init.rs} (98%) rename tests/{test_cargo_install.rs => install.rs} (95%) rename tests/{test_cargo_metadata.rs => metadata.rs} (98%) rename tests/{test_cargo_net_config.rs => net-config.rs} (94%) rename tests/{test_cargo_new.rs => new.rs} (94%) rename tests/{test_cargo_overrides.rs => overrides.rs} (98%) rename tests/{test_cargo_package.rs => package.rs} (96%) rename tests/{test_cargo_compile_path_deps.rs => path.rs} (98%) rename tests/{test_cargo_compile_plugins.rs => plugins.rs} (96%) rename tests/{test_cargo_profiles.rs => profiles.rs} (97%) rename tests/{test_cargo_publish.rs => publish.rs} (95%) rename tests/{test_cargo_read_manifest.rs => read-manifest.rs} (96%) rename tests/{test_cargo_registry.rs => registry.rs} (97%) rename tests/{test_cargo_run.rs => run.rs} (99%) rename tests/{test_cargo_rustc.rs => rustc.rs} (99%) rename tests/{test_cargo_rustdoc.rs => rustdoc.rs} (98%) rename tests/{test_cargo_compile_rustflags.rs => rustflags.rs} (98%) rename tests/{test_cargo_search.rs => search.rs} (95%) rename tests/{test_shell.rs => shell.rs} (93%) delete mode 100644 tests/support/paths.rs rename tests/{test_cargo_test.rs => test.rs} (99%) delete mode 100644 tests/tests.rs rename tests/{test_cargo_tool_paths.rs => tool-paths.rs} (94%) rename tests/{test_cargo_verify_project.rs => verify-project.rs} (92%) rename tests/{test_cargo_version.rs => version.rs} (90%) diff --git a/Cargo.lock b/Cargo.lock index aab3cb77d..5e86f8e00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,6 +4,7 @@ version = "0.11.0" dependencies = [ "advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "bufstream 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cargotest 0.1.0", "crates-io 0.2.0", "crossbeam 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "curl 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", @@ -60,6 +61,27 @@ name = "bufstream" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "cargotest" +version = "0.1.0" +dependencies = [ + "bufstream 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cargo 0.11.0", + "filetime 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "flate2 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", + "git2 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", + "hamcrest 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", + "tar 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", + "tempdir 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cmake" version = "0.1.16" diff --git a/Cargo.toml b/Cargo.toml index 6560c9ee3..4e6b62760 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,14 +49,9 @@ winapi = "0.2" hamcrest = "0.1" bufstream = "0.1" filetime = "0.1" +cargotest = { path = "tests/cargotest" } [[bin]] name = "cargo" test = false doc = false - -[[test]] -name = "tests" - -[[test]] -name = "resolve" diff --git a/tests/test_bad_config.rs b/tests/bad-config.rs similarity index 98% rename from tests/test_bad_config.rs rename to tests/bad-config.rs index 21c55fd6d..b88ea2f6b 100644 --- a/tests/test_bad_config.rs +++ b/tests/bad-config.rs @@ -1,5 +1,8 @@ -use support::{project, execs}; -use support::registry::Package; +extern crate cargotest; +extern crate hamcrest; + +use cargotest::support::{project, execs}; +use cargotest::support::registry::Package; use hamcrest::assert_that; #[test] diff --git a/tests/test_bad_manifest_path.rs b/tests/bad-manifest-path.rs similarity index 98% rename from tests/test_bad_manifest_path.rs rename to tests/bad-manifest-path.rs index ed10ed7ca..cd9a97ef5 100644 --- a/tests/test_bad_manifest_path.rs +++ b/tests/bad-manifest-path.rs @@ -1,4 +1,7 @@ -use support::{project, execs, main_file, basic_bin_manifest}; +extern crate hamcrest; +extern crate cargotest; + +use cargotest::support::{project, execs, main_file, basic_bin_manifest}; use hamcrest::{assert_that}; fn assert_not_a_cargo_toml(command: &str, manifest_path_argument: &str) { diff --git a/tests/test_cargo_bench.rs b/tests/bench.rs similarity index 96% rename from tests/test_cargo_bench.rs rename to tests/bench.rs index 1aae2b323..ea8f55740 100644 --- a/tests/test_cargo_bench.rs +++ b/tests/bench.rs @@ -1,13 +1,18 @@ +extern crate cargotest; +extern crate cargo; +extern crate hamcrest; + use std::str; -use support::{project, execs, basic_bin_manifest, basic_lib_manifest}; -use support::paths::CargoPathExt; -use hamcrest::{assert_that, existing_file}; use cargo::util::process; +use cargotest::is_nightly; +use cargotest::support::paths::CargoPathExt; +use cargotest::support::{project, execs, basic_bin_manifest, basic_lib_manifest}; +use hamcrest::{assert_that, existing_file}; #[test] fn cargo_bench_simple() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", &basic_bin_manifest("foo")) @@ -49,7 +54,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured #[test] fn bench_tarname() { - if !::is_nightly() { return } + if !is_nightly() { return } let prj = project("foo") .file("Cargo.toml" , r#" @@ -84,7 +89,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured #[test] fn cargo_bench_verbose() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", &basic_bin_manifest("foo")) @@ -111,7 +116,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured #[test] fn many_similar_names() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -149,7 +154,7 @@ fn many_similar_names() { #[test] fn cargo_bench_failing_test() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", &basic_bin_manifest("foo")) @@ -192,7 +197,7 @@ thread '
' panicked at 'assertion failed: \ #[test] fn bench_with_lib_dep() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -252,7 +257,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured #[test] fn bench_with_deep_lib_dep() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("bar") .file("Cargo.toml", r#" @@ -308,7 +313,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured #[test] fn external_bench_explicit() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -360,7 +365,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured #[test] fn external_bench_implicit() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -409,7 +414,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured #[test] fn dont_run_examples() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -429,7 +434,7 @@ fn dont_run_examples() { #[test] fn pass_through_command_line() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -476,7 +481,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured // tests in an rlib #[test] fn cargo_bench_twice() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("test_twice") .file("Cargo.toml", &basic_lib_manifest("test_twice")) @@ -500,7 +505,7 @@ fn cargo_bench_twice() { #[test] fn lib_bin_same_name() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -550,7 +555,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured #[test] fn lib_with_standard_name() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -603,7 +608,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured #[test] fn lib_with_standard_name2() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -647,7 +652,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured #[test] fn bench_dylib() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -719,7 +724,7 @@ test foo ... bench: [..] 0 ns/iter (+/- 0) test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured ")); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); assert_that(p.cargo("bench").arg("-v"), execs().with_status(0) .with_stderr(&format!("\ @@ -744,7 +749,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured #[test] fn bench_twice_with_build_cmd() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -790,7 +795,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured #[test] fn bench_with_examples() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("testbench") .file("Cargo.toml", r#" @@ -870,7 +875,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured #[test] fn test_a_bench() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -910,7 +915,7 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured #[test] fn test_bench_no_run() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -940,7 +945,7 @@ fn test_bench_no_run() { #[test] fn test_bench_multiple_packages() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" diff --git a/tests/test_cargo_build_auth.rs b/tests/build-auth.rs similarity index 97% rename from tests/test_cargo_build_auth.rs rename to tests/build-auth.rs index 050caf2cf..6ac2107df 100644 --- a/tests/test_cargo_build_auth.rs +++ b/tests/build-auth.rs @@ -1,13 +1,16 @@ +extern crate bufstream; +extern crate git2; +extern crate cargotest; +extern crate hamcrest; + use std::collections::HashSet; use std::io::prelude::*; use std::net::TcpListener; use std::thread; use bufstream::BufStream; -use git2; - -use support::{project, execs}; -use support::paths; +use cargotest::support::paths; +use cargotest::support::{project, execs}; use hamcrest::assert_that; // Test that HTTP auth is offered from `credential.helper` diff --git a/tests/test_cargo_build_lib.rs b/tests/build-lib.rs similarity index 94% rename from tests/test_cargo_build_lib.rs rename to tests/build-lib.rs index ee8e7fc97..7d4d44f15 100644 --- a/tests/test_cargo_build_lib.rs +++ b/tests/build-lib.rs @@ -1,5 +1,9 @@ +extern crate cargotest; +extern crate hamcrest; + use std::path::MAIN_SEPARATOR as SEP; -use support::{basic_bin_manifest, execs, project, ProjectBuilder}; + +use cargotest::support::{basic_bin_manifest, execs, project, ProjectBuilder}; use hamcrest::{assert_that}; fn verbose_output_for_lib(p: &ProjectBuilder) -> String { diff --git a/tests/test_cargo_compile_custom_build.rs b/tests/build-script.rs similarity index 98% rename from tests/test_cargo_compile_custom_build.rs rename to tests/build-script.rs index d51e4cae2..9b14cbc37 100644 --- a/tests/test_cargo_compile_custom_build.rs +++ b/tests/build-script.rs @@ -1,8 +1,12 @@ +extern crate cargotest; +extern crate hamcrest; + use std::fs::{self, File}; use std::io::prelude::*; -use support::{project, execs}; -use support::paths::CargoPathExt; +use cargotest::{rustc_host, is_nightly, sleep_ms}; +use cargotest::support::{project, execs}; +use cargotest::support::paths::CargoPathExt; use hamcrest::{assert_that, existing_file, existing_dir}; #[test] @@ -248,7 +252,7 @@ linked to by one package #[test] fn overrides_and_links() { - let target = ::rustc_host(); + let target = rustc_host(); let p = project("foo") .file("Cargo.toml", r#" @@ -302,7 +306,7 @@ fn overrides_and_links() { #[test] fn unused_overrides() { - let target = ::rustc_host(); + let target = rustc_host(); let p = project("foo") .file("Cargo.toml", r#" @@ -387,10 +391,10 @@ fn only_rerun_build_script() { assert_that(p.cargo_process("build").arg("-v"), execs().with_status(0)); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); File::create(&p.root().join("some-new-file")).unwrap(); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); assert_that(p.cargo("build").arg("-v"), execs().with_status(0) @@ -422,7 +426,7 @@ fn rebuild_continues_to_pass_env_vars() { } "#); a.build(); - a.root().move_into_the_past().unwrap(); + a.root().move_into_the_past(); let p = project("foo") .file("Cargo.toml", &format!(r#" @@ -446,10 +450,10 @@ fn rebuild_continues_to_pass_env_vars() { assert_that(p.cargo_process("build").arg("-v"), execs().with_status(0)); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); File::create(&p.root().join("some-new-file")).unwrap(); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); assert_that(p.cargo("build").arg("-v"), execs().with_status(0)); @@ -473,10 +477,10 @@ fn testing_and_such() { println!("build"); assert_that(p.cargo_process("build").arg("-v"), execs().with_status(0)); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); File::create(&p.root().join("src/lib.rs")).unwrap(); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); println!("test"); assert_that(p.cargo("test").arg("-vj1"), @@ -522,7 +526,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured #[test] fn propagation_of_l_flags() { - let target = ::rustc_host(); + let target = rustc_host(); let p = project("foo") .file("Cargo.toml", r#" [project] @@ -576,7 +580,7 @@ fn propagation_of_l_flags() { #[test] fn propagation_of_l_flags_new() { - let target = ::rustc_host(); + let target = rustc_host(); let p = project("foo") .file("Cargo.toml", r#" [project] @@ -667,7 +671,7 @@ fn build_deps_simple() { #[test] fn build_deps_not_for_normal() { - let target = ::rustc_host(); + let target = rustc_host(); let p = project("foo") .file("Cargo.toml", r#" [project] @@ -792,7 +796,7 @@ fn out_dir_is_preserved() { // Make the file assert_that(p.cargo_process("build").arg("-v"), execs().with_status(0)); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); // Change to asserting that it's there File::create(&p.root().join("build.rs")).unwrap().write_all(br#" @@ -803,7 +807,7 @@ fn out_dir_is_preserved() { File::open(&Path::new(&out).join("foo")).unwrap(); } "#).unwrap(); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); assert_that(p.cargo("build").arg("-v"), execs().with_status(0)); @@ -1282,7 +1286,7 @@ fn cfg_feedback() { #[test] fn cfg_override() { - let target = ::rustc_host(); + let target = rustc_host(); let p = project("foo") .file("Cargo.toml", r#" @@ -1439,7 +1443,7 @@ fn cfg_override_test() { .file(".cargo/config", &format!(r#" [target.{}.a] rustc-cfg = ["foo"] - "#, ::rustc_host())) + "#, rustc_host())) .file("src/lib.rs", r#" /// /// ``` @@ -1514,7 +1518,7 @@ fn cfg_override_doc() { rustc-cfg = ["foo"] [target.{target}.b] rustc-cfg = ["bar"] - "#, target = ::rustc_host())) + "#, target = rustc_host())) .file("build.rs", "") .file("src/lib.rs", r#" #[cfg(foo)] @@ -1677,7 +1681,7 @@ fn diamond_passes_args_only_once() { #[test] fn adding_an_override_invalidates() { - let target = ::rustc_host(); + let target = rustc_host(); let p = project("foo") .file("Cargo.toml", r#" [project] @@ -1717,7 +1721,7 @@ fn adding_an_override_invalidates() { #[test] fn changing_an_override_invalidates() { - let target = ::rustc_host(); + let target = rustc_host(); let p = project("foo") .file("Cargo.toml", r#" [project] @@ -1783,7 +1787,7 @@ fn rebuild_only_on_explicit_paths() { [RUNNING] `rustc src[..]lib.rs [..]` ")); - ::sleep_ms(1000); + sleep_ms(1000); File::create(p.root().join("foo")).unwrap(); File::create(p.root().join("bar")).unwrap(); @@ -1802,7 +1806,7 @@ fn rebuild_only_on_explicit_paths() { [FRESH] a v0.5.0 ([..]) ")); - ::sleep_ms(1000); + sleep_ms(1000); // random other files do not affect freshness println!("run baz"); @@ -1976,7 +1980,7 @@ fn custom_target_dir() { #[test] fn panic_abort_with_build_scripts() { - if !::is_nightly() { + if !is_nightly() { return } let p = project("foo") diff --git a/tests/test_cargo_compile.rs b/tests/build.rs similarity index 99% rename from tests/test_cargo_compile.rs rename to tests/build.rs index 0daee9e87..4f632113b 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/build.rs @@ -1,13 +1,19 @@ +extern crate cargo; +extern crate cargotest; +extern crate hamcrest; +extern crate tempdir; + use std::env; use std::fs::{self, File}; use std::io::prelude::*; -use tempdir::TempDir; -use support::{project, execs, main_file, basic_bin_manifest}; -use support::{ProjectBuilder}; -use hamcrest::{assert_that, existing_file, is_not}; -use support::paths::{CargoPathExt,root}; use cargo::util::process; +use cargotest::{is_nightly, rustc_host, sleep_ms}; +use cargotest::support::paths::{CargoPathExt,root}; +use cargotest::support::{ProjectBuilder}; +use cargotest::support::{project, execs, main_file, basic_bin_manifest}; +use hamcrest::{assert_that, existing_file, is_not}; +use tempdir::TempDir; #[test] fn cargo_compile_simple() { @@ -1449,7 +1455,7 @@ fn freshness_ignores_excluded() { .file("build.rs", "fn main() {}") .file("src/lib.rs", "pub fn bar() -> i32 { 1 }"); foo.build(); - foo.root().move_into_the_past().unwrap(); + foo.root().move_into_the_past(); assert_that(foo.cargo("build"), execs().with_status(0) @@ -1497,7 +1503,7 @@ fn rebuild_preserves_out_dir() { "#) .file("src/lib.rs", "pub fn bar() -> i32 { 1 }"); foo.build(); - foo.root().move_into_the_past().unwrap(); + foo.root().move_into_the_past(); assert_that(foo.cargo("build").env("FIRST", "1"), execs().with_status(0) @@ -1552,7 +1558,7 @@ fn recompile_space_in_name() { "#) .file("src/my lib.rs", ""); assert_that(foo.cargo_process("build"), execs().with_status(0)); - foo.root().move_into_the_past().unwrap(); + foo.root().move_into_the_past(); assert_that(foo.cargo("build"), execs().with_status(0).with_stdout("")); } @@ -1611,7 +1617,7 @@ Caused by: #[test] fn cargo_platform_specific_dependency() { - let host = ::rustc_host(); + let host = rustc_host(); let p = project("foo") .file("Cargo.toml", &format!(r#" [project] @@ -1785,7 +1791,7 @@ fn compile_then_delete() { assert_that(&p.bin("foo"), existing_file()); if cfg!(windows) { // On windows unlinking immediately after running often fails, so sleep - ::sleep_ms(100); + sleep_ms(100); } fs::remove_file(&p.bin("foo")).unwrap(); assert_that(p.cargo("run"), @@ -2202,7 +2208,7 @@ fn manifest_with_bom_is_ok() { #[test] fn panic_abort_compiles_with_panic_abort() { - if !::is_nightly() { + if !is_nightly() { return } let p = project("foo") diff --git a/tests/test_cargo.rs b/tests/cargo.rs similarity index 95% rename from tests/test_cargo.rs rename to tests/cargo.rs index 512dd956b..029000309 100644 --- a/tests/test_cargo.rs +++ b/tests/cargo.rs @@ -1,3 +1,6 @@ +extern crate cargotest; +extern crate hamcrest; + use std::env; use std::ffi::OsString; use std::fs::{self, File}; @@ -5,9 +8,9 @@ use std::io::prelude::*; use std::path::{Path, PathBuf}; use std::str; -use cargo_process; -use support::paths; -use support::{execs, project, mkdir_recursive, ProjectBuilder}; +use cargotest::cargo_process; +use cargotest::support::paths::{self, CargoPathExt}; +use cargotest::support::{execs, project, ProjectBuilder}; use hamcrest::{assert_that}; #[cfg_attr(windows,allow(dead_code))] @@ -21,7 +24,7 @@ enum FakeKind<'a> { fn fake_file(proj: ProjectBuilder, dir: &Path, name: &str, kind: FakeKind) -> ProjectBuilder { let path = proj.root().join(dir).join(&format!("{}{}", name, env::consts::EXE_SUFFIX)); - mkdir_recursive(path.parent().unwrap()).unwrap(); + path.parent().unwrap().mkdir_p(); match kind { FakeKind::Executable => { File::create(&path).unwrap(); @@ -78,7 +81,7 @@ fn list_command_looks_at_path() { #[cfg(unix)] #[test] fn list_command_resolves_symlinks() { - use support::cargo_dir; + use cargotest::support::cargo_dir; let proj = project("list-non-overlapping"); let proj = fake_file(proj, &Path::new("path-test"), "cargo-2", diff --git a/tests/cargotest/Cargo.toml b/tests/cargotest/Cargo.toml new file mode 100644 index 000000000..5f35c034d --- /dev/null +++ b/tests/cargotest/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "cargotest" +version = "0.1.0" +authors = ["Alex Crichton "] + +[lib] +path = "lib.rs" + +[dependencies] +bufstream = "0.1" +cargo = { path = "../.." } +filetime = "0.1" +flate2 = "0.2" +git2 = "0.4" +hamcrest = "0.1" +kernel32-sys = "0.2" +libc = "0.2" +log = "0.3" +rustc-serialize = "0.3" +tar = "0.4" +tempdir = "0.3" +term = "0.4.4" +url = "1.1" +winapi = "0.2" diff --git a/tests/cargotest/install.rs b/tests/cargotest/install.rs new file mode 100644 index 000000000..12a396972 --- /dev/null +++ b/tests/cargotest/install.rs @@ -0,0 +1,30 @@ +use std::fmt; +use std::path::{PathBuf, Path}; + +use hamcrest::{Matcher, MatchResult, existing_file}; +use support::paths; + +pub use self::InstalledExe as has_installed_exe; + +pub fn cargo_home() -> PathBuf { + paths::home().join(".cargo") +} + +pub struct InstalledExe(pub &'static str); + +fn exe(name: &str) -> String { + if cfg!(windows) {format!("{}.exe", name)} else {name.to_string()} +} + +impl> Matcher

for InstalledExe { + fn matches(&self, path: P) -> MatchResult { + let path = path.as_ref().join("bin").join(exe(self.0)); + existing_file().matches(&path) + } +} + +impl fmt::Display for InstalledExe { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "installed exe `{}`", self.0) + } +} diff --git a/tests/cargotest/lib.rs b/tests/cargotest/lib.rs new file mode 100644 index 000000000..7053f4839 --- /dev/null +++ b/tests/cargotest/lib.rs @@ -0,0 +1,62 @@ +#![deny(warnings)] + +extern crate bufstream; +extern crate cargo; +extern crate filetime; +extern crate flate2; +extern crate git2; +extern crate hamcrest; +extern crate libc; +extern crate rustc_serialize; +extern crate tar; +extern crate tempdir; +extern crate term; +extern crate url; +#[cfg(windows)] extern crate kernel32; +#[cfg(windows)] extern crate winapi; + +#[macro_use] +extern crate log; + +use cargo::util::Rustc; +use std::ffi::OsStr; +use std::time::Duration; + +pub mod support; +pub mod install; + +thread_local!(pub static RUSTC: Rustc = Rustc::new("rustc").unwrap()); + +pub fn rustc_host() -> String { + RUSTC.with(|r| r.host.clone()) +} + +pub fn is_nightly() -> bool { + RUSTC.with(|r| { + r.verbose_version.contains("-nightly") || + r.verbose_version.contains("-dev") + }) +} + +pub fn process>(t: T) -> cargo::util::ProcessBuilder { + let mut p = cargo::util::process(t.as_ref()); + p.cwd(&support::paths::root()) + .env_remove("CARGO_HOME") + .env("HOME", support::paths::home()) + .env("CARGO_HOME", support::paths::home().join(".cargo")) + .env_remove("RUSTC") + .env_remove("RUSTFLAGS") + .env_remove("XDG_CONFIG_HOME") // see #2345 + .env("GIT_CONFIG_NOSYSTEM", "1") // keep trying to sandbox ourselves + .env_remove("CARGO_TARGET_DIR") // we assume 'target' + .env_remove("MSYSTEM"); // assume cmd.exe everywhere on windows + return p +} + +pub fn cargo_process() -> cargo::util::ProcessBuilder { + process(&support::cargo_dir().join("cargo")) +} + +pub fn sleep_ms(ms: u64) { + std::thread::sleep(Duration::from_millis(ms)); +} diff --git a/tests/support/git.rs b/tests/cargotest/support/git.rs similarity index 52% rename from tests/support/git.rs rename to tests/cargotest/support/git.rs index 729c18413..393157a62 100644 --- a/tests/support/git.rs +++ b/tests/cargotest/support/git.rs @@ -17,12 +17,12 @@ pub fn repo(p: &Path) -> RepoBuilder { RepoBuilder::init(p) } impl RepoBuilder { pub fn init(p: &Path) -> RepoBuilder { - fs::create_dir_all(p.parent().unwrap()).unwrap(); - let repo = git2::Repository::init(p).unwrap(); + t!(fs::create_dir_all(p.parent().unwrap())); + let repo = t!(git2::Repository::init(p)); { - let mut config = repo.config().unwrap(); - config.set_str("user.name", "name").unwrap(); - config.set_str("user.email", "email").unwrap(); + let mut config = t!(repo.config()); + t!(config.set_str("user.name", "name")); + t!(config.set_str("user.email", "email")); } RepoBuilder { repo: repo, files: Vec::new() } } @@ -35,22 +35,22 @@ impl RepoBuilder { pub fn nocommit_file(self, path: &str, contents: &str) -> RepoBuilder { let dst = self.repo.workdir().unwrap().join(path); - fs::create_dir_all(dst.parent().unwrap()).unwrap(); - File::create(&dst).unwrap().write_all(contents.as_bytes()).unwrap(); + t!(fs::create_dir_all(dst.parent().unwrap())); + t!(t!(File::create(&dst)).write_all(contents.as_bytes())); self } pub fn build(&self) { - let mut index = self.repo.index().unwrap(); + let mut index = t!(self.repo.index()); for file in self.files.iter() { - index.add_path(file).unwrap(); + t!(index.add_path(file)); } - index.write().unwrap(); - let id = index.write_tree().unwrap(); - let tree = self.repo.find_tree(id).unwrap(); - let sig = self.repo.signature().unwrap(); - self.repo.commit(Some("HEAD"), &sig, &sig, - "Initial commit", &tree, &[]).unwrap(); + t!(index.write()); + let id = t!(index.write_tree()); + let tree = t!(self.repo.find_tree(id)); + let sig = t!(self.repo.signature()); + t!(self.repo.commit(Some("HEAD"), &sig, &sig, + "Initial commit", &tree, &[])); } pub fn root(&self) -> &Path { @@ -69,10 +69,10 @@ pub fn new(name: &str, callback: F) -> Result git_project = callback(git_project); git_project.build(); - let repo = git2::Repository::init(&git_project.root()).unwrap(); - let mut cfg = repo.config().unwrap(); - cfg.set_str("user.email", "foo@bar.com").unwrap(); - cfg.set_str("user.name", "Foo Bar").unwrap(); + let repo = t!(git2::Repository::init(&git_project.root())); + let mut cfg = t!(repo.config()); + t!(cfg.set_str("user.email", "foo@bar.com")); + t!(cfg.set_str("user.name", "Foo Bar")); drop(cfg); add(&repo); commit(&repo); @@ -83,51 +83,51 @@ pub fn add(repo: &git2::Repository) { // FIXME(libgit2/libgit2#2514): apparently add_all will add all submodules // as well, and then fail b/c they're a directory. As a stopgap, we just // ignore all submodules. - let mut s = repo.submodules().unwrap(); + let mut s = t!(repo.submodules()); for submodule in s.iter_mut() { - submodule.add_to_index(false).unwrap(); + t!(submodule.add_to_index(false)); } - let mut index = repo.index().unwrap(); - index.add_all(["*"].iter(), git2::ADD_DEFAULT, + let mut index = t!(repo.index()); + t!(index.add_all(["*"].iter(), git2::ADD_DEFAULT, Some(&mut (|a, _b| { if s.iter().any(|s| a.starts_with(s.path())) {1} else {0} - }))).unwrap(); - index.write().unwrap(); + })))); + t!(index.write()); } pub fn add_submodule<'a>(repo: &'a git2::Repository, url: &str, path: &Path) -> git2::Submodule<'a> { let path = path.to_str().unwrap().replace(r"\", "/"); - let mut s = repo.submodule(url, Path::new(&path), false).unwrap(); - let subrepo = s.open().unwrap(); - subrepo.remote_add_fetch("origin", "refs/heads/*:refs/heads/*").unwrap(); - let mut origin = subrepo.find_remote("origin").unwrap(); - origin.fetch(&[], None, None).unwrap(); - subrepo.checkout_head(None).unwrap(); - s.add_finalize().unwrap(); + let mut s = t!(repo.submodule(url, Path::new(&path), false)); + let subrepo = t!(s.open()); + t!(subrepo.remote_add_fetch("origin", "refs/heads/*:refs/heads/*")); + let mut origin = t!(subrepo.find_remote("origin")); + t!(origin.fetch(&[], None, None)); + t!(subrepo.checkout_head(None)); + t!(s.add_finalize()); return s; } pub fn commit(repo: &git2::Repository) -> git2::Oid { - let tree_id = repo.index().unwrap().write_tree().unwrap(); - let sig = repo.signature().unwrap(); + let tree_id = t!(t!(repo.index()).write_tree()); + let sig = t!(repo.signature()); let mut parents = Vec::new(); match repo.head().ok().map(|h| h.target().unwrap()) { - Some(parent) => parents.push(repo.find_commit(parent).unwrap()), + Some(parent) => parents.push(t!(repo.find_commit(parent))), None => {} } let parents = parents.iter().collect::>(); - repo.commit(Some("HEAD"), &sig, &sig, "test", - &repo.find_tree(tree_id).unwrap(), - &parents).unwrap() + t!(repo.commit(Some("HEAD"), &sig, &sig, "test", + &t!(repo.find_tree(tree_id)), + &parents)) } pub fn tag(repo: &git2::Repository, name: &str) { let head = repo.head().unwrap().target().unwrap(); - repo.tag(name, - &repo.find_object(head, None).unwrap(), - &repo.signature().unwrap(), - "make a new tag", - false).unwrap(); + t!(repo.tag(name, + &t!(repo.find_object(head, None)), + &t!(repo.signature()), + "make a new tag", + false)); } diff --git a/tests/support/mod.rs b/tests/cargotest/support/mod.rs similarity index 90% rename from tests/support/mod.rs rename to tests/cargotest/support/mod.rs index 856a3f954..47ae3f4eb 100644 --- a/tests/support/mod.rs +++ b/tests/cargotest/support/mod.rs @@ -19,6 +19,14 @@ use cargo::util::process; use support::paths::CargoPathExt; +#[macro_export] +macro_rules! t { + ($e:expr) => (match $e { + Ok(e) => e, + Err(e) => panic!("{} failed with {}", stringify!($e), e), + }) +} + pub mod paths; pub mod git; pub mod registry; @@ -40,17 +48,14 @@ impl FileBuilder { FileBuilder { path: path, body: body.to_string() } } - fn mk(&self) -> Result<(), String> { - try!(mkdir_recursive(&self.dirname())); + fn mk(&self) { + self.dirname().mkdir_p(); - let mut file = try!( - fs::File::create(&self.path) - .with_err_msg(format!("Could not create file; path={}", - self.path.display()))); + let mut file = fs::File::create(&self.path).unwrap_or_else(|e| { + panic!("could not create file {}: {}", self.path.display(), e) + }); - file.write_all(self.body.as_bytes()) - .with_err_msg(format!("Could not write to file; path={}", - self.path.display())) + t!(file.write_all(self.body.as_bytes())); } fn dirname(&self) -> &Path { @@ -70,21 +75,15 @@ impl SymlinkBuilder { } #[cfg(unix)] - fn mk(&self) -> Result<(), String> { - try!(mkdir_recursive(&self.dirname())); - - os::unix::fs::symlink(&self.dst, &self.src) - .with_err_msg(format!("Could not create symlink; dst={} src={}", - self.dst.display(), self.src.display())) + fn mk(&self) { + self.dirname().mkdir_p(); + t!(os::unix::fs::symlink(&self.dst, &self.src)); } #[cfg(windows)] - fn mk(&self) -> Result<(), String> { - try!(mkdir_recursive(&self.dirname())); - - os::windows::fs::symlink_file(&self.dst, &self.src) - .with_err_msg(format!("Could not create symlink; dst={} src={}", - self.dst.display(), self.src.display())) + fn mk(&self) { + self.dirname().mkdir_p(); + t!(os::windows::fs::symlink_file(&self.dst, &self.src)); } fn dirname(&self) -> &Path { @@ -167,36 +166,24 @@ impl ProjectBuilder { // TODO: return something different than a ProjectBuilder pub fn build(&self) -> &ProjectBuilder { - match self.build_with_result() { - Err(e) => panic!(e), - _ => return self - } - } - - pub fn build_with_result(&self) -> Result<(), String> { // First, clean the directory if it already exists - try!(self.rm_root()); + self.rm_root(); // Create the empty directory - try!(mkdir_recursive(&self.root)); + self.root.mkdir_p(); for file in self.files.iter() { - try!(file.mk()); + file.mk(); } for symlink in self.symlinks.iter() { - try!(symlink.mk()); + symlink.mk(); } - - Ok(()) + self } - fn rm_root(&self) -> Result<(), String> { - if self.root.c_exists() { - rmdir_recursive(&self.root) - } else { - Ok(()) - } + fn rm_root(&self) { + self.root.rm_rf() } } @@ -207,18 +194,6 @@ pub fn project(name: &str) -> ProjectBuilder { // === Helpers === -pub fn mkdir_recursive(path: &Path) -> Result<(), String> { - fs::create_dir_all(path) - .with_err_msg(format!("could not create directory; path={}", - path.display())) -} - -pub fn rmdir_recursive(path: &Path) -> Result<(), String> { - path.rm_rf() - .with_err_msg(format!("could not rm directory; path={}", - path.display())) -} - pub fn main_file(println: &str, deps: &[&str]) -> String { let mut buf = String::new(); diff --git a/tests/cargotest/support/paths.rs b/tests/cargotest/support/paths.rs new file mode 100644 index 000000000..62e3b548d --- /dev/null +++ b/tests/cargotest/support/paths.rs @@ -0,0 +1,146 @@ +use std::env; +use std::cell::Cell; +use std::fs; +use std::io::{self, ErrorKind}; +use std::path::{Path, PathBuf}; +use std::sync::{Once, ONCE_INIT}; +use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; + +use filetime::{self, FileTime}; + +static CARGO_INTEGRATION_TEST_DIR : &'static str = "cit"; +static NEXT_ID: AtomicUsize = ATOMIC_USIZE_INIT; + +thread_local!(static TASK_ID: usize = NEXT_ID.fetch_add(1, Ordering::SeqCst)); + +fn init() { + static GLOBAL_INIT: Once = ONCE_INIT; + thread_local!(static LOCAL_INIT: Cell = Cell::new(false)); + GLOBAL_INIT.call_once(|| { + global_root().mkdir_p(); + }); + LOCAL_INIT.with(|i| { + if i.get() { + return + } + i.set(true); + root().rm_rf(); + home().mkdir_p(); + }) +} + +fn global_root() -> PathBuf { + let mut path = t!(env::current_exe()); + path.pop(); // chop off exe name + path.pop(); // chop off 'debug' + + // If `cargo test` is run manually then our path looks like + // `target/debug/foo`, in which case our `path` is already pointing at + // `target`. If, however, `cargo test --target $target` is used then the + // output is `target/$target/debug/foo`, so our path is pointing at + // `target/$target`. Here we conditionally pop the `$target` name. + if path.file_name().and_then(|s| s.to_str()) != Some("target") { + path.pop(); + } + + path.join(CARGO_INTEGRATION_TEST_DIR) +} + +pub fn root() -> PathBuf { + init(); + global_root().join(&TASK_ID.with(|my_id| format!("t{}", my_id))) +} + +pub fn home() -> PathBuf { + root().join("home") +} + +pub trait CargoPathExt { + fn rm_rf(&self); + fn mkdir_p(&self); + fn move_into_the_past(&self); +} + +impl CargoPathExt for Path { + /* Technically there is a potential race condition, but we don't + * care all that much for our tests + */ + fn rm_rf(&self) { + if !self.exists() { + return + } + + for file in t!(fs::read_dir(self)) { + let file = t!(file).path(); + + if file.is_dir() { + file.rm_rf(); + } else { + // On windows we can't remove a readonly file, and git will + // often clone files as readonly. As a result, we have some + // special logic to remove readonly files on windows. + do_op(&file, "remove file", |p| fs::remove_file(p)); + } + } + do_op(self, "remove dir", |p| fs::remove_dir(p)); + } + + fn mkdir_p(&self) { + fs::create_dir_all(self).unwrap_or_else(|e| { + panic!("failed to mkdir_p {}: {}", self.display(), e) + }) + } + + fn move_into_the_past(&self) { + if self.is_file() { + time_travel(self); + } else { + recurse(self, &self.join("target")); + } + + fn recurse(p: &Path, bad: &Path) { + if p.is_file() { + time_travel(p) + } else if !p.starts_with(bad) { + for f in t!(fs::read_dir(p)) { + let f = t!(f).path(); + recurse(&f, bad); + } + } + } + + fn time_travel(path: &Path) { + let stat = t!(path.metadata()); + + let mtime = FileTime::from_last_modification_time(&stat); + let newtime = mtime.seconds_relative_to_1970() - 3600; + let nanos = mtime.nanoseconds(); + let newtime = FileTime::from_seconds_since_1970(newtime, nanos); + + // Sadly change_file_times has a failure mode where a readonly file + // cannot have its times changed on windows. + do_op(path, "set file times", + |path| filetime::set_file_times(path, newtime, newtime)); + } + } +} + +fn do_op(path: &Path, desc: &str, mut f: F) + where F: FnMut(&Path) -> io::Result<()> +{ + match f(path) { + Ok(()) => {} + Err(ref e) if cfg!(windows) && + e.kind() == ErrorKind::PermissionDenied => { + let mut p = t!(path.metadata()).permissions(); + p.set_readonly(false); + t!(fs::set_permissions(path, p)); + f(path).unwrap_or_else(|e| { + panic!("failed to {} {}: {}", desc, path.display(), e); + }) + } + Err(e) => { + panic!("failed to {} {}: {}", desc, path.display(), e); + } + } +} diff --git a/tests/support/registry.rs b/tests/cargotest/support/registry.rs similarity index 83% rename from tests/support/registry.rs rename to tests/cargotest/support/registry.rs index da8532440..a783c6c0b 100644 --- a/tests/support/registry.rs +++ b/tests/cargotest/support/registry.rs @@ -39,15 +39,15 @@ struct Dependency { fn init() { let config = paths::home().join(".cargo/config"); - fs::create_dir_all(config.parent().unwrap()).unwrap(); + t!(fs::create_dir_all(config.parent().unwrap())); if fs::metadata(&config).is_ok() { return } - File::create(&config).unwrap().write_all(format!(r#" + t!(t!(File::create(&config)).write_all(format!(r#" [registry] index = "{reg}" token = "api-token" - "#, reg = registry()).as_bytes()).unwrap(); + "#, reg = registry()).as_bytes())); // Init a new registry repo(®istry_path()) @@ -135,8 +135,7 @@ impl Package { }).collect::>(); let cksum = { let mut c = Vec::new(); - File::open(&self.archive_dst()).unwrap() - .read_to_end(&mut c).unwrap(); + t!(t!(File::open(&self.archive_dst())).read_to_end(&mut c)); cksum(&c) }; let mut dep = HashMap::new(); @@ -159,25 +158,25 @@ impl Package { let dst = registry_path().join(&file); let mut prev = String::new(); let _ = File::open(&dst).and_then(|mut f| f.read_to_string(&mut prev)); - fs::create_dir_all(dst.parent().unwrap()).unwrap(); - File::create(&dst).unwrap() - .write_all((prev + &line[..] + "\n").as_bytes()).unwrap(); + t!(fs::create_dir_all(dst.parent().unwrap())); + t!(t!(File::create(&dst)) + .write_all((prev + &line[..] + "\n").as_bytes())); // Add the new file to the index - let repo = git2::Repository::open(®istry_path()).unwrap(); - let mut index = repo.index().unwrap(); - index.add_path(Path::new(&file)).unwrap(); - index.write().unwrap(); - let id = index.write_tree().unwrap(); + let repo = t!(git2::Repository::open(®istry_path())); + let mut index = t!(repo.index()); + t!(index.add_path(Path::new(&file))); + t!(index.write()); + let id = t!(index.write_tree()); // Commit this change - let tree = repo.find_tree(id).unwrap(); - let sig = repo.signature().unwrap(); - let parent = repo.refname_to_id("refs/heads/master").unwrap(); - let parent = repo.find_commit(parent).unwrap(); - repo.commit(Some("HEAD"), &sig, &sig, - "Another commit", &tree, - &[&parent]).unwrap(); + let tree = t!(repo.find_tree(id)); + let sig = t!(repo.signature()); + let parent = t!(repo.refname_to_id("refs/heads/master")); + let parent = t!(repo.find_commit(parent)); + t!(repo.commit(Some("HEAD"), &sig, &sig, + "Another commit", &tree, + &[&parent])); } fn make_archive(&self) { @@ -204,8 +203,8 @@ impl Package { } let dst = self.archive_dst(); - fs::create_dir_all(dst.parent().unwrap()).unwrap(); - let f = File::create(&dst).unwrap(); + t!(fs::create_dir_all(dst.parent().unwrap())); + let f = t!(File::create(&dst)); let mut a = Builder::new(GzEncoder::new(f, Default)); self.append(&mut a, "Cargo.toml", &manifest); if self.files.is_empty() { @@ -220,10 +219,10 @@ impl Package { fn append(&self, ar: &mut Builder, file: &str, contents: &str) { let mut header = Header::new_ustar(); header.set_size(contents.len() as u64); - header.set_path(format!("{}-{}/{}", self.name, self.vers, file)).unwrap(); + t!(header.set_path(format!("{}-{}/{}", self.name, self.vers, file))); header.set_cksum(); - ar.append(&header, contents.as_bytes()).unwrap(); + t!(ar.append(&header, contents.as_bytes())); } pub fn archive_dst(&self) -> PathBuf { diff --git a/tests/test_cargo_cfg.rs b/tests/cfg.rs similarity index 96% rename from tests/test_cargo_cfg.rs rename to tests/cfg.rs index 7f85d58a9..457cab3af 100644 --- a/tests/test_cargo_cfg.rs +++ b/tests/cfg.rs @@ -1,12 +1,16 @@ +extern crate cargo; +extern crate cargotest; +extern crate hamcrest; + use std::str::FromStr; use std::fmt; use cargo::util::{Cfg, CfgExpr}; +use cargotest::{is_nightly, rustc_host}; +use cargotest::support::registry::Package; +use cargotest::support::{project, execs}; use hamcrest::assert_that; -use support::{project, execs}; -use support::registry::Package; - macro_rules! c { ($a:ident) => ( Cfg::Name(stringify!($a).to_string()) @@ -134,7 +138,7 @@ fn cfg_matches() { #[test] fn cfg_easy() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -162,7 +166,7 @@ fn cfg_easy() { #[test] fn dont_include() { - if !::is_nightly() { return } + if !is_nightly() { return } let other_family = if cfg!(unix) {"windows"} else {"unix"}; let p = project("foo") @@ -191,7 +195,7 @@ fn dont_include() { #[test] fn works_through_the_registry() { - if !::is_nightly() { return } + if !is_nightly() { return } Package::new("foo", "0.1.0").publish(); Package::new("bar", "0.1.0") @@ -276,7 +280,7 @@ Caused by: #[test] fn multiple_match_ok() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", &format!(r#" @@ -298,7 +302,7 @@ fn multiple_match_ok() { [target.{}.dependencies] b = {{ path = 'b' }} - "#, ::rustc_host())) + "#, rustc_host())) .file("src/lib.rs", "extern crate b;") .file("b/Cargo.toml", r#" [package] @@ -313,7 +317,7 @@ fn multiple_match_ok() { #[test] fn any_ok() { - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" diff --git a/tests/test_cargo_clean.rs b/tests/clean.rs similarity index 97% rename from tests/test_cargo_clean.rs rename to tests/clean.rs index 25f77423b..ea45ceb57 100644 --- a/tests/test_cargo_clean.rs +++ b/tests/clean.rs @@ -1,7 +1,10 @@ +extern crate hamcrest; +extern crate cargotest; + use std::env; -use support::{git, project, execs, main_file, basic_bin_manifest}; -use support::registry::Package; +use cargotest::support::{git, project, execs, main_file, basic_bin_manifest}; +use cargotest::support::registry::Package; use hamcrest::{assert_that, existing_dir, existing_file, is_not}; #[test] diff --git a/tests/test_cargo_concurrent.rs b/tests/concurrent.rs similarity index 96% rename from tests/test_cargo_concurrent.rs rename to tests/concurrent.rs index fb74771c7..4499f1d3a 100644 --- a/tests/test_cargo_concurrent.rs +++ b/tests/concurrent.rs @@ -1,3 +1,8 @@ +extern crate cargo; +extern crate cargotest; +extern crate git2; +extern crate hamcrest; + use std::{env, str}; use std::fs::{self, File}; use std::io::Write; @@ -5,14 +10,12 @@ use std::net::TcpListener; use std::process::Stdio; use std::thread; -use git2; +use cargotest::install::{has_installed_exe, cargo_home}; +use cargotest::support::git; +use cargotest::support::registry::Package; +use cargotest::support::{execs, project}; use hamcrest::{assert_that, existing_file}; -use support::{execs, project}; -use support::git; -use support::registry::Package; -use test_cargo_install::{cargo_home, has_installed_exe}; - fn pkg(name: &str, vers: &str) { Package::new(name, vers) .file("src/main.rs", "fn main() {{}}") @@ -64,8 +67,8 @@ fn concurrent_installs() { pkg("foo", "0.0.1"); pkg("bar", "0.0.1"); - let mut a = ::cargo_process().arg("install").arg("foo").build_command(); - let mut b = ::cargo_process().arg("install").arg("bar").build_command(); + let mut a = cargotest::cargo_process().arg("install").arg("foo").build_command(); + let mut b = cargotest::cargo_process().arg("install").arg("bar").build_command(); a.stdout(Stdio::piped()).stderr(Stdio::piped()); b.stdout(Stdio::piped()).stderr(Stdio::piped()); diff --git a/tests/test_cargo_config.rs b/tests/config.rs similarity index 86% rename from tests/test_cargo_config.rs rename to tests/config.rs index c3aa0563d..39c85dabd 100644 --- a/tests/test_cargo_config.rs +++ b/tests/config.rs @@ -1,4 +1,7 @@ -use support::{project, execs}; +extern crate hamcrest; +extern crate cargotest; + +use cargotest::support::{project, execs}; use hamcrest::assert_that; #[test] diff --git a/tests/test_cargo_cross_compile.rs b/tests/cross-compile.rs similarity index 98% rename from tests/test_cargo_cross_compile.rs rename to tests/cross-compile.rs index 095aaeba3..7578454d7 100644 --- a/tests/test_cargo_cross_compile.rs +++ b/tests/cross-compile.rs @@ -1,8 +1,13 @@ +extern crate cargo; +extern crate cargotest; +extern crate hamcrest; + use std::env; -use support::{project, execs, basic_bin_manifest}; -use hamcrest::{assert_that, existing_file}; use cargo::util::process; +use cargotest::{is_nightly, rustc_host}; +use cargotest::support::{project, execs, basic_bin_manifest}; +use hamcrest::{assert_that, existing_file}; fn disabled() -> bool { // First, disable if ./configure requested so @@ -167,7 +172,7 @@ fn simple_deps() { #[test] fn plugin_deps() { if disabled() { return } - if !::is_nightly() { return } + if !is_nightly() { return } let foo = project("foo") .file("Cargo.toml", r#" @@ -245,7 +250,7 @@ fn plugin_deps() { #[test] fn plugin_to_the_max() { if disabled() { return } - if !::is_nightly() { return } + if !is_nightly() { return } let foo = project("foo") .file("Cargo.toml", r#" @@ -369,7 +374,7 @@ fn linker_and_ar() { #[test] fn plugin_with_extra_dylib_dep() { if disabled() { return } - if !::is_nightly() { return } + if !is_nightly() { return } let foo = project("foo") .file("Cargo.toml", r#" @@ -603,7 +608,7 @@ fn build_script_needed_for_host_and_target() { if disabled() { return } let target = alternate(); - let host = ::rustc_host(); + let host = rustc_host(); let p = project("foo") .file("Cargo.toml", r#" [package] @@ -801,7 +806,7 @@ fn build_script_with_platform_specific_dependencies() { if disabled() { return } let target = alternate(); - let host = ::rustc_host(); + let host = rustc_host(); let p = project("foo") .file("Cargo.toml", r#" [package] @@ -852,7 +857,7 @@ fn platform_specific_dependencies_do_not_leak() { if disabled() { return } let target = alternate(); - let host = ::rustc_host(); + let host = rustc_host(); let p = project("foo") .file("Cargo.toml", r#" [package] @@ -898,7 +903,7 @@ fn platform_specific_variables_reflected_in_build_scripts() { if disabled() { return } let target = alternate(); - let host = ::rustc_host(); + let host = rustc_host(); let p = project("foo") .file("Cargo.toml", &format!(r#" [package] diff --git a/tests/test_cargo_death.rs b/tests/death.rs similarity index 96% rename from tests/test_cargo_death.rs rename to tests/death.rs index 097dbdc12..52922a746 100644 --- a/tests/test_cargo_death.rs +++ b/tests/death.rs @@ -1,8 +1,13 @@ +extern crate cargotest; +extern crate kernel32; +extern crate libc; +extern crate winapi; + use std::net::TcpListener; use std::io::{self, Read}; use std::process::{Stdio, Child}; -use support::project; +use cargotest::support::project; #[cfg(unix)] fn enabled() -> bool { diff --git a/tests/test_cargo_doc.rs b/tests/doc.rs similarity index 98% rename from tests/test_cargo_doc.rs rename to tests/doc.rs index a39fed883..9acd0e56f 100644 --- a/tests/test_cargo_doc.rs +++ b/tests/doc.rs @@ -1,7 +1,11 @@ +extern crate cargotest; +extern crate hamcrest; + use std::str; use std::fs; -use support::{project, execs, path2url}; +use cargotest::{is_nightly, rustc_host}; +use cargotest::support::{project, execs, path2url}; use hamcrest::{assert_that, existing_file, existing_dir, is_not}; #[test] @@ -274,7 +278,7 @@ fn doc_same_name() { fn doc_target() { const TARGET: &'static str = "arm-unknown-linux-gnueabihf"; - if !::is_nightly() { return } + if !is_nightly() { return } let p = project("foo") .file("Cargo.toml", r#" @@ -369,7 +373,7 @@ fn target_specific_documented() { a = {{ path = "a" }} [target.{}.dependencies] a = {{ path = "a" }} - "#, ::rustc_host())) + "#, rustc_host())) .file("src/lib.rs", " extern crate a; diff --git a/tests/test_cargo_features.rs b/tests/features.rs similarity index 98% rename from tests/test_cargo_features.rs rename to tests/features.rs index a46d51f83..5fcefaae6 100644 --- a/tests/test_cargo_features.rs +++ b/tests/features.rs @@ -1,8 +1,12 @@ +#[macro_use] +extern crate cargotest; +extern crate hamcrest; + use std::fs::File; use std::io::prelude::*; -use support::{project, execs}; -use support::paths::CargoPathExt; +use cargotest::support::paths::CargoPathExt; +use cargotest::support::{project, execs}; use hamcrest::assert_that; #[test] @@ -548,7 +552,7 @@ fn many_features_no_rebuilds() { [COMPILING] a v0.1.0 ({dir}/a) [COMPILING] b v0.1.0 ({dir}) ", dir = p.url()))); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); assert_that(p.cargo("build").arg("-v"), execs().with_status(0).with_stderr("\ @@ -665,7 +669,7 @@ fn everything_in_the_lockfile() { assert_that(p.cargo_process("fetch"), execs().with_status(0)); let loc = p.root().join("Cargo.lock"); let mut lockfile = String::new(); - File::open(&loc).unwrap().read_to_string(&mut lockfile).unwrap(); + t!(t!(File::open(&loc)).read_to_string(&mut lockfile)); assert!(lockfile.contains(r#"name = "d1""#), "d1 not found\n{}", lockfile); assert!(lockfile.contains(r#"name = "d2""#), "d2 not found\n{}", lockfile); assert!(lockfile.contains(r#"name = "d3""#), "d3 not found\n{}", lockfile); diff --git a/tests/test_cargo_fetch.rs b/tests/fetch.rs similarity index 82% rename from tests/test_cargo_fetch.rs rename to tests/fetch.rs index d2d530291..a6ced09f9 100644 --- a/tests/test_cargo_fetch.rs +++ b/tests/fetch.rs @@ -1,4 +1,7 @@ -use support::{project, execs}; +extern crate cargotest; +extern crate hamcrest; + +use cargotest::support::{project, execs}; use hamcrest::assert_that; #[test] diff --git a/tests/test_cargo_freshness.rs b/tests/freshness.rs similarity index 96% rename from tests/test_cargo_freshness.rs rename to tests/freshness.rs index e049c26e9..f5173bebc 100644 --- a/tests/test_cargo_freshness.rs +++ b/tests/freshness.rs @@ -1,8 +1,12 @@ +extern crate cargotest; +extern crate hamcrest; + use std::fs::{self, File}; use std::io::prelude::*; -use support::{project, execs, path2url}; -use support::paths::CargoPathExt; +use cargotest::sleep_ms; +use cargotest::support::{project, execs, path2url}; +use cargotest::support::paths::CargoPathExt; use hamcrest::{assert_that, existing_file}; #[test] @@ -26,8 +30,8 @@ fn modifying_and_moving() { assert_that(p.cargo("build"), execs().with_status(0).with_stdout("")); - p.root().move_into_the_past().unwrap(); - p.root().join("target").move_into_the_past().unwrap(); + p.root().move_into_the_past(); + p.root().join("target").move_into_the_past(); File::create(&p.root().join("src/a.rs")).unwrap() .write_all(b"#[allow(unused)]fn main() {}").unwrap(); @@ -65,7 +69,7 @@ fn modify_only_some_files() { ", dir = path2url(p.root())))); assert_that(p.cargo("test"), execs().with_status(0)); - ::sleep_ms(1000); + sleep_ms(1000); assert_that(&p.bin("foo"), existing_file()); @@ -74,7 +78,7 @@ fn modify_only_some_files() { File::create(&lib).unwrap().write_all(b"invalid rust code").unwrap(); File::create(&bin).unwrap().write_all(b"#[allow(unused)]fn foo() {}").unwrap(); - lib.move_into_the_past().unwrap(); + lib.move_into_the_past(); // Make sure the binary is rebuilt, not the lib assert_that(p.cargo("build"), @@ -195,8 +199,8 @@ fn rebuild_tests_if_lib_changes() { execs().with_status(0)); File::create(&p.root().join("src/lib.rs")).unwrap(); - p.root().move_into_the_past().unwrap(); - p.root().join("target").move_into_the_past().unwrap(); + p.root().move_into_the_past(); + p.root().join("target").move_into_the_past(); assert_that(p.cargo("build"), execs().with_status(0)); diff --git a/tests/test_cargo_generate_lockfile.rs b/tests/generate-lockfile.rs similarity index 98% rename from tests/test_cargo_generate_lockfile.rs rename to tests/generate-lockfile.rs index b4d7236c2..ad4d207f7 100644 --- a/tests/test_cargo_generate_lockfile.rs +++ b/tests/generate-lockfile.rs @@ -1,7 +1,10 @@ +extern crate cargotest; +extern crate hamcrest; + use std::fs::{self, File}; use std::io::prelude::*; -use support::{project, execs}; +use cargotest::support::{project, execs}; use hamcrest::{assert_that, existing_file, is_not}; #[test] diff --git a/tests/test_cargo_compile_git_deps.rs b/tests/git.rs similarity index 98% rename from tests/test_cargo_compile_git_deps.rs rename to tests/git.rs index 14825ba36..ef7f1b0cd 100644 --- a/tests/test_cargo_compile_git_deps.rs +++ b/tests/git.rs @@ -1,12 +1,17 @@ +extern crate cargo; +extern crate cargotest; +extern crate git2; +extern crate hamcrest; + use std::fs::{self, File}; use std::io::prelude::*; use std::path::Path; -use git2; -use support::{git, project, execs, main_file, path2url}; -use support::paths::{self, CargoPathExt}; -use hamcrest::{assert_that,existing_file}; use cargo::util::process; +use cargotest::{sleep_ms, RUSTC}; +use cargotest::support::paths::{self, CargoPathExt}; +use cargotest::support::{git, project, execs, main_file, path2url}; +use hamcrest::{assert_that,existing_file}; #[test] fn cargo_compile_simple_git_dep() { @@ -534,7 +539,7 @@ fn recompilation() { println!("compile after commit"); assert_that(p.cargo("build"), execs().with_stdout("")); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); // Update the dependency and carry on! assert_that(p.cargo("update"), @@ -635,7 +640,7 @@ fn update_with_shared_deps() { git::add(&repo); git::commit(&repo); - ::sleep_ms(1000); + sleep_ms(1000); // By default, not transitive updates println!("dep1 update"); @@ -838,7 +843,7 @@ fn stale_cached_version() { git::add(&repo); git::commit(&repo); - ::sleep_ms(1000); + sleep_ms(1000); let rev = repo.revparse_single("HEAD").unwrap().id(); @@ -940,7 +945,7 @@ fn dep_with_changed_submodule() { git::add(&repo); git::commit(&repo); - ::sleep_ms(1000); + sleep_ms(1000); // Update the dependency and carry on! println!("update"); assert_that(project.cargo("update").arg("-v"), @@ -1035,9 +1040,9 @@ fn git_build_cmd_freshness() { src/bar.rs ") }).unwrap(); - foo.root().move_into_the_past().unwrap(); + foo.root().move_into_the_past(); - ::sleep_ms(1000); + sleep_ms(1000); assert_that(foo.cargo("build"), execs().with_status(0) @@ -1125,7 +1130,7 @@ fn git_repo_changing_no_rebuild() { .file("src/main.rs", "fn main() {}") .file("build.rs", "fn main() {}"); p1.build(); - p1.root().move_into_the_past().unwrap(); + p1.root().move_into_the_past(); assert_that(p1.cargo("build"), execs().with_stderr(&format!("\ [UPDATING] git repository `{bar}` @@ -1209,7 +1214,7 @@ fn git_dep_build_cmd() { "#) }).unwrap(); - p.root().join("bar").move_into_the_past().unwrap(); + p.root().join("bar").move_into_the_past(); assert_that(p.cargo("build"), execs().with_status(0)); @@ -1707,7 +1712,7 @@ fn lints_are_suppressed() { #[test] fn denied_lints_are_allowed() { - let enabled = super::RUSTC.with(|r| r.cap_lints); + let enabled = RUSTC.with(|r| r.cap_lints); if !enabled { return } let a = git::new("a", |p| { diff --git a/tests/test_cargo_init.rs b/tests/init.rs similarity index 98% rename from tests/test_cargo_init.rs rename to tests/init.rs index b902cb0bf..0e180bb45 100644 --- a/tests/test_cargo_init.rs +++ b/tests/init.rs @@ -1,11 +1,16 @@ +extern crate cargotest; +extern crate cargo; +extern crate tempdir; +extern crate hamcrest; + use std::fs::{self, File}; use std::io::prelude::*; use std::env; -use tempdir::TempDir; -use support::{execs, paths, cargo_dir}; -use hamcrest::{assert_that, existing_file, existing_dir, is_not}; use cargo::util::{process, ProcessBuilder}; +use cargotest::support::{execs, paths, cargo_dir}; +use hamcrest::{assert_that, existing_file, existing_dir, is_not}; +use tempdir::TempDir; fn cargo_process(s: &str) -> ProcessBuilder { let mut p = process(&cargo_dir().join("cargo")); diff --git a/tests/test_cargo_install.rs b/tests/install.rs similarity index 95% rename from tests/test_cargo_install.rs rename to tests/install.rs index bc5be24c0..71b61b5e9 100644 --- a/tests/test_cargo_install.rs +++ b/tests/install.rs @@ -1,21 +1,20 @@ -use std::fmt; +extern crate cargo; +extern crate cargotest; +extern crate hamcrest; + use std::fs::{self, File}; use std::io::prelude::*; -use std::path::{Path, PathBuf}; -use support::paths::CargoPathExt; use cargo::util::ProcessBuilder; -use hamcrest::{assert_that, existing_file, is_not, Matcher, MatchResult}; - -use support::{project, execs}; -use support::paths; -use support::registry::Package; -use support::git; - -pub use self::InstalledExe as has_installed_exe; +use cargotest::install::{cargo_home, has_installed_exe}; +use cargotest::support::git; +use cargotest::support::paths; +use cargotest::support::registry::Package; +use cargotest::support::{project, execs}; +use hamcrest::{assert_that, is_not}; fn cargo_process(s: &str) -> ProcessBuilder { - let mut p = ::cargo_process(); + let mut p = cargotest::cargo_process(); p.arg(s); return p } @@ -30,29 +29,6 @@ fn pkg(name: &str, vers: &str) { .publish() } -fn exe(name: &str) -> String { - if cfg!(windows) {format!("{}.exe", name)} else {name.to_string()} -} - -pub fn cargo_home() -> PathBuf { - paths::home().join(".cargo") -} - -pub struct InstalledExe(pub &'static str); - -impl> Matcher

for InstalledExe { - fn matches(&self, path: P) -> MatchResult { - let path = path.as_ref().join("bin").join(exe(self.0)); - existing_file().matches(&path) - } -} - -impl fmt::Display for InstalledExe { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "installed exe `{}`", self.0) - } -} - #[test] fn simple() { pkg("foo", "0.0.1"); @@ -722,8 +698,8 @@ fn do_not_rebuilds_on_local_install() { warning: be sure to add `[..]` to your PATH to be able to run the installed binaries ")); - assert!(p.build_dir().c_exists()); - assert!(p.release_bin("foo").c_exists()); + assert!(p.build_dir().exists()); + assert!(p.release_bin("foo").exists()); assert_that(cargo_home(), has_installed_exe("foo")); } diff --git a/tests/test_cargo_metadata.rs b/tests/metadata.rs similarity index 98% rename from tests/test_cargo_metadata.rs rename to tests/metadata.rs index eee4486c5..478acf883 100644 --- a/tests/test_cargo_metadata.rs +++ b/tests/metadata.rs @@ -1,7 +1,9 @@ -use hamcrest::assert_that; -use support::registry::Package; -use support::{project, execs, basic_bin_manifest, main_file}; +extern crate cargotest; +extern crate hamcrest; +use hamcrest::assert_that; +use cargotest::support::registry::Package; +use cargotest::support::{project, execs, basic_bin_manifest, main_file}; #[test] fn cargo_metadata_simple() { diff --git a/tests/test_cargo_net_config.rs b/tests/net-config.rs similarity index 94% rename from tests/test_cargo_net_config.rs rename to tests/net-config.rs index 92389f625..d4fdb3b12 100644 --- a/tests/test_cargo_net_config.rs +++ b/tests/net-config.rs @@ -1,4 +1,7 @@ -use support::{project, execs}; +extern crate cargotest; +extern crate hamcrest; + +use cargotest::support::{project, execs}; use hamcrest::assert_that; #[test] diff --git a/tests/test_cargo_new.rs b/tests/new.rs similarity index 94% rename from tests/test_cargo_new.rs rename to tests/new.rs index fc2d73916..23b637d3d 100644 --- a/tests/test_cargo_new.rs +++ b/tests/new.rs @@ -1,16 +1,20 @@ +extern crate cargo; +extern crate cargotest; +extern crate hamcrest; +extern crate tempdir; + use std::fs::{self, File}; use std::io::prelude::*; use std::env; -use tempdir::TempDir; - -use support::{execs, paths}; -use support::paths::CargoPathExt; -use hamcrest::{assert_that, existing_file, existing_dir, is_not}; use cargo::util::ProcessBuilder; +use cargotest::process; +use cargotest::support::{execs, paths}; +use hamcrest::{assert_that, existing_file, existing_dir, is_not}; +use tempdir::TempDir; fn cargo_process(s: &str) -> ProcessBuilder { - let mut p = ::cargo_process(); + let mut p = cargotest::cargo_process(); p.arg(s); return p; } @@ -230,10 +234,10 @@ fn finds_author_email() { #[test] fn finds_author_git() { - ::process("git").args(&["config", "--global", "user.name", "bar"]) - .exec().unwrap(); - ::process("git").args(&["config", "--global", "user.email", "baz"]) - .exec().unwrap(); + process("git").args(&["config", "--global", "user.name", "bar"]) + .exec().unwrap(); + process("git").args(&["config", "--global", "user.email", "baz"]) + .exec().unwrap(); assert_that(cargo_process("new").arg("foo").env("USER", "foo"), execs().with_status(0)); @@ -278,10 +282,10 @@ fn finds_git_author() { #[test] fn author_prefers_cargo() { - ::process("git").args(&["config", "--global", "user.name", "foo"]) - .exec().unwrap(); - ::process("git").args(&["config", "--global", "user.email", "bar"]) - .exec().unwrap(); + process("git").args(&["config", "--global", "user.name", "foo"]) + .exec().unwrap(); + process("git").args(&["config", "--global", "user.email", "bar"]) + .exec().unwrap(); let root = paths::root(); fs::create_dir(&root.join(".cargo")).unwrap(); File::create(&root.join(".cargo/config")).unwrap().write_all(br#" @@ -298,7 +302,7 @@ fn author_prefers_cargo() { let mut contents = String::new(); File::open(&toml).unwrap().read_to_string(&mut contents).unwrap(); assert!(contents.contains(r#"authors = ["new-foo "]"#)); - assert!(!root.join("foo/.gitignore").c_exists()); + assert!(!root.join("foo/.gitignore").exists()); } #[test] @@ -317,7 +321,7 @@ fn git_prefers_command_line() { .cwd(td.path()) .env("USER", "foo"), execs().with_status(0)); - assert!(td.path().join("foo/.gitignore").c_exists()); + assert!(td.path().join("foo/.gitignore").exists()); } #[test] diff --git a/tests/test_cargo_overrides.rs b/tests/overrides.rs similarity index 98% rename from tests/test_cargo_overrides.rs rename to tests/overrides.rs index 4a53938cc..2f87638f0 100644 --- a/tests/test_cargo_overrides.rs +++ b/tests/overrides.rs @@ -1,9 +1,11 @@ -use hamcrest::assert_that; +extern crate cargotest; +extern crate hamcrest; -use support::registry::{registry, Package}; -use support::{execs, project}; -use support::git; -use support::paths; +use cargotest::support::git; +use cargotest::support::paths; +use cargotest::support::registry::{registry, Package}; +use cargotest::support::{execs, project}; +use hamcrest::assert_that; #[test] fn override_simple() { diff --git a/tests/test_cargo_package.rs b/tests/package.rs similarity index 96% rename from tests/test_cargo_package.rs rename to tests/package.rs index 1e6a82c2f..f9a810fb4 100644 --- a/tests/test_cargo_package.rs +++ b/tests/package.rs @@ -1,13 +1,18 @@ +extern crate cargotest; +extern crate flate2; +extern crate git2; +extern crate hamcrest; +extern crate tar; + use std::fs::File; use std::io::prelude::*; use std::path::Path; +use cargotest::cargo_process; +use cargotest::support::{project, execs, paths, git, path2url}; use flate2::read::GzDecoder; -use git2; -use tar::Archive; - -use support::{project, execs, paths, git, path2url}; use hamcrest::{assert_that, existing_file}; +use tar::Archive; #[test] fn simple() { @@ -145,7 +150,7 @@ fn package_verbose() { "#) .file("a/src/lib.rs", ""); p.build(); - let mut cargo = ::cargo_process(); + let mut cargo = cargo_process(); cargo.cwd(p.root()); assert_that(cargo.clone().arg("build"), execs().with_status(0)); @@ -276,7 +281,7 @@ fn package_new_git_repo() { p.build(); git2::Repository::init(&p.root()).unwrap(); - assert_that(::cargo_process().arg("package").cwd(p.root()) + assert_that(cargo_process().arg("package").cwd(p.root()) .arg("--no-verify").arg("-v"), execs().with_status(0).with_stderr("\ [WARNING] manifest has no description[..] @@ -313,7 +318,7 @@ fn package_git_submodule() { repository.reset(&repository.revparse_single("HEAD").unwrap(), git2::ResetType::Hard, None).unwrap(); - assert_that(::cargo_process().arg("package").cwd(project.root()) + assert_that(cargo_process().arg("package").cwd(project.root()) .arg("--no-verify").arg("-v"), execs().with_status(0).with_stderr_contains("[ARCHIVING] bar/Makefile")); } @@ -335,7 +340,7 @@ fn no_duplicates_from_modified_tracked_files() { File::create(p.root().join("src/main.rs")).unwrap().write_all(r#" fn main() { println!("A change!"); } "#.as_bytes()).unwrap(); - let mut cargo = ::cargo_process(); + let mut cargo = cargo_process(); cargo.cwd(p.root()); assert_that(cargo.clone().arg("build"), execs().with_status(0)); assert_that(cargo.arg("package").arg("--list"), diff --git a/tests/test_cargo_compile_path_deps.rs b/tests/path.rs similarity index 98% rename from tests/test_cargo_compile_path_deps.rs rename to tests/path.rs index e2f2d36e2..97cc21322 100644 --- a/tests/test_cargo_compile_path_deps.rs +++ b/tests/path.rs @@ -1,10 +1,15 @@ +extern crate cargo; +extern crate cargotest; +extern crate hamcrest; + use std::fs::{self, File}; use std::io::prelude::*; -use support::{project, execs, main_file}; -use support::paths::{self, CargoPathExt}; -use hamcrest::{assert_that, existing_file}; use cargo::util::process; +use cargotest::sleep_ms; +use cargotest::support::paths::{self, CargoPathExt}; +use cargotest::support::{project, execs, main_file}; +use hamcrest::{assert_that, existing_file}; #[test] fn cargo_compile_with_nested_deps_shorthand() { @@ -279,7 +284,7 @@ fn no_rebuild_dependency() { // This time we shouldn't compile bar assert_that(p.cargo("build"), execs().with_stdout("")); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); p.build(); // rebuild the files (rewriting them in the process) assert_that(p.cargo("build"), @@ -352,7 +357,7 @@ fn deep_dependencies_trigger_rebuild() { // // We base recompilation off mtime, so sleep for at least a second to ensure // that this write will change the mtime. - ::sleep_ms(1000); + sleep_ms(1000); File::create(&p.root().join("baz/src/baz.rs")).unwrap().write_all(br#" pub fn baz() { println!("hello!"); } "#).unwrap(); @@ -365,7 +370,7 @@ fn deep_dependencies_trigger_rebuild() { p.url()))); // Make sure an update to bar doesn't trigger baz - ::sleep_ms(1000); + sleep_ms(1000); File::create(&p.root().join("bar/src/bar.rs")).unwrap().write_all(br#" extern crate baz; pub fn bar() { println!("hello!"); baz::baz(); } @@ -481,7 +486,7 @@ fn nested_deps_recompile() { [COMPILING] foo v0.5.0 ({})\n", bar, p.url()))); - ::sleep_ms(1000); + sleep_ms(1000); File::create(&p.root().join("src/foo.rs")).unwrap().write_all(br#" fn main() {} @@ -686,7 +691,7 @@ fn path_dep_build_cmd() { "#); p.build(); - p.root().join("bar").move_into_the_past().unwrap(); + p.root().join("bar").move_into_the_past(); assert_that(p.cargo("build"), execs().with_stderr(&format!("[COMPILING] bar v0.5.0 ({}/bar)\n\ diff --git a/tests/test_cargo_compile_plugins.rs b/tests/plugins.rs similarity index 96% rename from tests/test_cargo_compile_plugins.rs rename to tests/plugins.rs index 4c3c3183e..7adffed9e 100644 --- a/tests/test_cargo_compile_plugins.rs +++ b/tests/plugins.rs @@ -1,12 +1,16 @@ +extern crate cargotest; +extern crate hamcrest; + use std::fs; use std::env; -use support::{project, execs}; +use cargotest::{is_nightly, rustc_host}; +use cargotest::support::{project, execs}; use hamcrest::assert_that; #[test] fn plugin_to_the_max() { - if !::is_nightly() { return } + if !is_nightly() { return } let foo = project("foo") .file("Cargo.toml", r#" @@ -84,7 +88,7 @@ fn plugin_to_the_max() { #[test] fn plugin_with_dynamic_native_dependency() { - if !::is_nightly() { return } + if !is_nightly() { return } let build = project("builder") .file("Cargo.toml", r#" @@ -227,7 +231,7 @@ fn doctest_a_plugin() { // See #1515 #[test] fn native_plugin_dependency_with_custom_ar_linker() { - let target = ::rustc_host(); + let target = rustc_host(); let foo = project("foo") .file("Cargo.toml", r#" diff --git a/tests/test_cargo_profiles.rs b/tests/profiles.rs similarity index 97% rename from tests/test_cargo_profiles.rs rename to tests/profiles.rs index 5b2c268a2..6edecf465 100644 --- a/tests/test_cargo_profiles.rs +++ b/tests/profiles.rs @@ -1,7 +1,10 @@ +extern crate cargotest; +extern crate hamcrest; + use std::env; use std::path::MAIN_SEPARATOR as SEP; -use support::{project, execs}; +use cargotest::support::{project, execs}; use hamcrest::assert_that; #[test] diff --git a/tests/test_cargo_publish.rs b/tests/publish.rs similarity index 95% rename from tests/test_cargo_publish.rs rename to tests/publish.rs index 0c3aa6115..37299bc0d 100644 --- a/tests/test_cargo_publish.rs +++ b/tests/publish.rs @@ -1,18 +1,22 @@ +extern crate cargotest; +extern crate flate2; +extern crate hamcrest; +extern crate tar; +extern crate url; + use std::io::prelude::*; use std::fs::{self, File}; use std::io::SeekFrom; use std::path::PathBuf; +use cargotest::support::git::repo; +use cargotest::support::paths; +use cargotest::support::{project, execs}; use flate2::read::GzDecoder; +use hamcrest::assert_that; use tar::Archive; use url::Url; -use support::{project, execs}; -use support::paths; -use support::git::repo; - -use hamcrest::assert_that; - fn registry_path() -> PathBuf { paths::root().join("registry") } fn registry() -> Url { Url::from_file_path(&*registry_path()).ok().unwrap() } fn upload_path() -> PathBuf { paths::root().join("upload") } diff --git a/tests/test_cargo_read_manifest.rs b/tests/read-manifest.rs similarity index 96% rename from tests/test_cargo_read_manifest.rs rename to tests/read-manifest.rs index d239a0f02..cc24f3f6c 100644 --- a/tests/test_cargo_read_manifest.rs +++ b/tests/read-manifest.rs @@ -1,4 +1,7 @@ -use support::{project, execs, main_file, basic_bin_manifest}; +extern crate cargotest; +extern crate hamcrest; + +use cargotest::support::{project, execs, main_file, basic_bin_manifest}; use hamcrest::{assert_that}; fn remove_all_whitespace(s: &str) -> String { diff --git a/tests/test_cargo_registry.rs b/tests/registry.rs similarity index 97% rename from tests/test_cargo_registry.rs rename to tests/registry.rs index 2e2efad35..cb517c91c 100644 --- a/tests/test_cargo_registry.rs +++ b/tests/registry.rs @@ -1,11 +1,15 @@ +#[macro_use] +extern crate cargotest; +extern crate hamcrest; + use std::fs::{self, File}; use std::io::prelude::*; -use support::{project, execs}; -use support::paths::{self, CargoPathExt}; -use support::registry::{self, Package}; -use support::git; - +use cargotest::cargo_process; +use cargotest::support::git; +use cargotest::support::paths::{self, CargoPathExt}; +use cargotest::support::registry::{self, Package}; +use cargotest::support::{project, execs}; use hamcrest::assert_that; #[test] @@ -282,7 +286,7 @@ fn lockfile_locks() { ", dir = p.url()))); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); Package::new("bar", "0.0.2").publish(); assert_that(p.cargo("build"), @@ -318,7 +322,7 @@ fn lockfile_locks_transitively() { ", dir = p.url()))); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); Package::new("baz", "0.0.2").publish(); Package::new("bar", "0.0.2").dep("baz", "*").publish(); @@ -439,9 +443,9 @@ fn update_with_lockfile_if_packages_missing() { Package::new("bar", "0.0.1").publish(); assert_that(p.cargo("build"), execs().with_status(0)); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); - paths::home().join(".cargo/registry").rm_rf().unwrap(); + paths::home().join(".cargo/registry").rm_rf(); assert_that(p.cargo("build"), execs().with_status(0).with_stderr("\ [UPDATING] registry `[..]` @@ -471,7 +475,7 @@ fn update_lockfile() { Package::new("bar", "0.0.2").publish(); Package::new("bar", "0.0.3").publish(); - paths::home().join(".cargo/registry").rm_rf().unwrap(); + paths::home().join(".cargo/registry").rm_rf(); println!("0.0.2 update"); assert_that(p.cargo("update") .arg("-p").arg("bar").arg("--precise").arg("0.0.2"), @@ -560,7 +564,7 @@ fn dev_dependency_not_used() { fn login_with_no_cargo_dir() { let home = paths::home().join("new-home"); fs::create_dir(&home).unwrap(); - assert_that(::cargo_process().arg("login").arg("foo").arg("-v"), + assert_that(cargo_process().arg("login").arg("foo").arg("-v"), execs().with_status(0)); } @@ -623,7 +627,7 @@ fn updating_a_dep() { ", dir = p.url()))); - File::create(&p.root().join("a/Cargo.toml")).unwrap().write_all(br#" + t!(File::create(&p.root().join("a/Cargo.toml"))).write_all(br#" [project] name = "a" version = "0.0.1" @@ -678,7 +682,7 @@ fn git_and_registry_dep() { Package::new("a", "0.0.1").publish(); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); assert_that(p.cargo("build"), execs().with_status(0).with_stderr(&format!("\ [UPDATING] [..] @@ -689,7 +693,7 @@ fn git_and_registry_dep() { [COMPILING] foo v0.0.1 ({dir}) ", dir = p.url()))); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); println!("second"); assert_that(p.cargo("build"), diff --git a/tests/test_cargo_run.rs b/tests/run.rs similarity index 99% rename from tests/test_cargo_run.rs rename to tests/run.rs index 151ee8b09..53ab2b0cb 100644 --- a/tests/test_cargo_run.rs +++ b/tests/run.rs @@ -1,6 +1,9 @@ +extern crate cargotest; +extern crate hamcrest; + use std::path::MAIN_SEPARATOR as SEP; -use support::{project, execs, path2url}; +use cargotest::support::{project, execs, path2url}; use hamcrest::{assert_that, existing_file}; #[test] diff --git a/tests/test_cargo_rustc.rs b/tests/rustc.rs similarity index 99% rename from tests/test_cargo_rustc.rs rename to tests/rustc.rs index afc132e7f..e65d713d4 100644 --- a/tests/test_cargo_rustc.rs +++ b/tests/rustc.rs @@ -1,7 +1,9 @@ -use std::path::MAIN_SEPARATOR as SEP; +extern crate cargotest; +extern crate hamcrest; -use support::{execs, project}; +use std::path::MAIN_SEPARATOR as SEP; +use cargotest::support::{execs, project}; use hamcrest::assert_that; const CARGO_RUSTC_ERROR: &'static str = diff --git a/tests/test_cargo_rustdoc.rs b/tests/rustdoc.rs similarity index 98% rename from tests/test_cargo_rustdoc.rs rename to tests/rustdoc.rs index ed9e6fc9b..535c0d151 100644 --- a/tests/test_cargo_rustdoc.rs +++ b/tests/rustdoc.rs @@ -1,5 +1,9 @@ +extern crate cargotest; +extern crate hamcrest; + use std::path::MAIN_SEPARATOR as SEP; -use support::{execs, project}; + +use cargotest::support::{execs, project}; use hamcrest::{assert_that}; #[test] diff --git a/tests/test_cargo_compile_rustflags.rs b/tests/rustflags.rs similarity index 98% rename from tests/test_cargo_compile_rustflags.rs rename to tests/rustflags.rs index 8984b527a..264c47219 100644 --- a/tests/test_cargo_compile_rustflags.rs +++ b/tests/rustflags.rs @@ -1,6 +1,11 @@ +extern crate cargotest; +extern crate hamcrest; + use std::io::Write; use std::fs::{self, File}; -use support::{project, execs, paths}; + +use cargotest::rustc_host; +use cargotest::support::{project, execs, paths}; use hamcrest::assert_that; #[test] @@ -184,7 +189,7 @@ fn env_rustflags_normal_source_with_target() { #[bench] fn run1(_ben: &mut test::Bencher) { }"#); p.build(); - let ref host = ::rustc_host(); + let ref host = rustc_host(); // Use RUSTFLAGS to pass an argument that will generate an error assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus") @@ -224,7 +229,7 @@ fn env_rustflags_build_script_with_target() { "#); p.build(); - let host = ::rustc_host(); + let host = rustc_host(); assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo") .arg("--target").arg(host), execs().with_status(0)); @@ -263,7 +268,7 @@ fn env_rustflags_build_script_dep_with_target() { foo.build(); bar.build(); - let host = ::rustc_host(); + let host = rustc_host(); assert_that(foo.cargo("build").env("RUSTFLAGS", "--cfg foo") .arg("--target").arg(host), execs().with_status(0)); @@ -291,7 +296,7 @@ fn env_rustflags_plugin_with_target() { "#); p.build(); - let host = ::rustc_host(); + let host = rustc_host(); assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo") .arg("--target").arg(host), execs().with_status(0)); @@ -335,7 +340,7 @@ fn env_rustflags_plugin_dep_with_target() { foo.build(); bar.build(); - let host = ::rustc_host(); + let host = rustc_host(); assert_that(foo.cargo("build").env("RUSTFLAGS", "--cfg foo") .arg("--target").arg(host), execs().with_status(0)); @@ -598,7 +603,7 @@ fn build_rustflags_normal_source_with_target() { "#); p.build(); - let ref host = ::rustc_host(); + let ref host = rustc_host(); // Use RUSTFLAGS to pass an argument that will generate an error assert_that(p.cargo("build") @@ -642,7 +647,7 @@ fn build_rustflags_build_script_with_target() { "#); p.build(); - let host = ::rustc_host(); + let host = rustc_host(); assert_that(p.cargo("build") .arg("--target").arg(host), execs().with_status(0)); @@ -685,7 +690,7 @@ fn build_rustflags_build_script_dep_with_target() { foo.build(); bar.build(); - let host = ::rustc_host(); + let host = rustc_host(); assert_that(foo.cargo("build") .arg("--target").arg(host), execs().with_status(0)); @@ -717,7 +722,7 @@ fn build_rustflags_plugin_with_target() { "#); p.build(); - let host = ::rustc_host(); + let host = rustc_host(); assert_that(p.cargo("build") .arg("--target").arg(host), execs().with_status(0)); @@ -765,7 +770,7 @@ fn build_rustflags_plugin_dep_with_target() { foo.build(); bar.build(); - let host = ::rustc_host(); + let host = rustc_host(); assert_that(foo.cargo("build") .arg("--target").arg(host), execs().with_status(0)); diff --git a/tests/test_cargo_search.rs b/tests/search.rs similarity index 95% rename from tests/test_cargo_search.rs rename to tests/search.rs index 181088e0e..19f04a33b 100644 --- a/tests/test_cargo_search.rs +++ b/tests/search.rs @@ -1,15 +1,18 @@ +extern crate cargo; +extern crate cargotest; +extern crate hamcrest; +extern crate url; + use std::fs::{self, File}; use std::io::prelude::*; use std::path::PathBuf; -use url::Url; - use cargo::util::ProcessBuilder; -use support::execs; -use support::paths; -use support::git::repo; - +use cargotest::support::execs; +use cargotest::support::git::repo; +use cargotest::support::paths; use hamcrest::assert_that; +use url::Url; fn registry_path() -> PathBuf { paths::root().join("registry") } fn registry() -> Url { Url::from_file_path(&*registry_path()).ok().unwrap() } @@ -34,7 +37,7 @@ fn setup() { } fn cargo_process(s: &str) -> ProcessBuilder { - let mut b = ::cargo_process(); + let mut b = cargotest::cargo_process(); b.arg(s); return b } diff --git a/tests/test_shell.rs b/tests/shell.rs similarity index 93% rename from tests/test_shell.rs rename to tests/shell.rs index 039b5c5e1..40471f0bf 100644 --- a/tests/test_shell.rs +++ b/tests/shell.rs @@ -1,14 +1,18 @@ +extern crate cargo; +extern crate cargotest; +extern crate hamcrest; +extern crate term; + use std::io::prelude::*; use std::io; use std::sync::{Arc, Mutex}; -use term::{Terminal, TerminfoTerminal, color}; -use hamcrest::{assert_that}; -use cargo::core::shell::{Shell, ShellConfig}; use cargo::core::shell::ColorConfig::{Auto,Always, Never}; +use cargo::core::shell::{Shell, ShellConfig}; use cargo::util::CargoResult; - -use support::{Tap, execs, shell_writes}; +use cargotest::support::{Tap, execs, shell_writes}; +use hamcrest::{assert_that}; +use term::{Terminal, TerminfoTerminal, color}; struct Sink(Arc>>); @@ -83,7 +87,7 @@ fn color_explicitly_enabled() { #[test] fn no_term() { // Verify that shell creation is successful when $TERM does not exist. - assert_that(::cargo_process().env_remove("TERM"), + assert_that(cargotest::cargo_process().env_remove("TERM"), execs().with_stderr("")); } diff --git a/tests/support/paths.rs b/tests/support/paths.rs deleted file mode 100644 index 837cface0..000000000 --- a/tests/support/paths.rs +++ /dev/null @@ -1,166 +0,0 @@ -use std::env; -use std::cell::Cell; -use std::fs; -use std::io::{self, ErrorKind}; -use std::path::{Path, PathBuf}; -use std::sync::{Once, ONCE_INIT}; -use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; - -use filetime::{self, FileTime}; - -static CARGO_INTEGRATION_TEST_DIR : &'static str = "cit"; -static NEXT_ID: AtomicUsize = ATOMIC_USIZE_INIT; - -thread_local!(static TASK_ID: usize = NEXT_ID.fetch_add(1, Ordering::SeqCst)); - -fn init() { - static GLOBAL_INIT: Once = ONCE_INIT; - thread_local!(static LOCAL_INIT: Cell = Cell::new(false)); - GLOBAL_INIT.call_once(|| { - global_root().mkdir_p().unwrap(); - }); - LOCAL_INIT.with(|i| { - if i.get() { - return - } - i.set(true); - root().rm_rf().unwrap(); - home().mkdir_p().unwrap(); - }) -} - -fn global_root() -> PathBuf { - let mut path = env::current_exe().unwrap(); - path.pop(); // chop off exe name - path.pop(); // chop off 'debug' - - // If `cargo test` is run manually then our path looks like - // `target/debug/foo`, in which case our `path` is already pointing at - // `target`. If, however, `cargo test --target $target` is used then the - // output is `target/$target/debug/foo`, so our path is pointing at - // `target/$target`. Here we conditionally pop the `$target` name. - if path.file_name().and_then(|s| s.to_str()) != Some("target") { - path.pop(); - } - - path.join(CARGO_INTEGRATION_TEST_DIR) -} - -pub fn root() -> PathBuf { - init(); - global_root().join(&TASK_ID.with(|my_id| format!("t{}", my_id))) -} - -pub fn home() -> PathBuf { - root().join("home") -} - -pub trait CargoPathExt { - fn rm_rf(&self) -> io::Result<()>; - fn mkdir_p(&self) -> io::Result<()>; - fn move_into_the_past(&self) -> io::Result<()>; - - // cargo versions of the standard PathExt trait - fn c_exists(&self) -> bool; - fn c_is_file(&self) -> bool; - fn c_is_dir(&self) -> bool; - fn c_metadata(&self) -> io::Result; -} - -impl CargoPathExt for Path { - /* Technically there is a potential race condition, but we don't - * care all that much for our tests - */ - fn rm_rf(&self) -> io::Result<()> { - if self.c_exists() { - for file in fs::read_dir(self).unwrap() { - let file = try!(file).path(); - - if file.c_is_dir() { - try!(file.rm_rf()); - } else { - // On windows we can't remove a readonly file, and git will - // often clone files as readonly. As a result, we have some - // special logic to remove readonly files on windows. - match fs::remove_file(&file) { - Ok(()) => {} - Err(ref e) if cfg!(windows) && - e.kind() == ErrorKind::PermissionDenied => { - let mut p = file.c_metadata().unwrap().permissions(); - p.set_readonly(false); - fs::set_permissions(&file, p).unwrap(); - try!(fs::remove_file(&file)); - } - Err(e) => return Err(e) - } - } - } - fs::remove_dir(self) - } else { - Ok(()) - } - } - - fn mkdir_p(&self) -> io::Result<()> { - fs::create_dir_all(self) - } - - fn move_into_the_past(&self) -> io::Result<()> { - if self.c_is_file() { - try!(time_travel(self)); - } else { - try!(recurse(self, &self.join("target"))); - } - return Ok(()); - - fn recurse(p: &Path, bad: &Path) -> io::Result<()> { - if p.c_is_file() { - time_travel(p) - } else if p.starts_with(bad) { - Ok(()) - } else { - for f in try!(fs::read_dir(p)) { - let f = try!(f).path(); - try!(recurse(&f, bad)); - } - Ok(()) - } - } - - fn time_travel(path: &Path) -> io::Result<()> { - let stat = try!(path.c_metadata()); - - let mtime = FileTime::from_last_modification_time(&stat); - let newtime = mtime.seconds_relative_to_1970() - 3600; - let nanos = mtime.nanoseconds(); - let newtime = FileTime::from_seconds_since_1970(newtime, nanos); - - // Sadly change_file_times has a failure mode where a readonly file - // cannot have its times changed on windows. - match filetime::set_file_times(path, newtime, newtime) { - Err(ref e) if e.kind() == io::ErrorKind::PermissionDenied => {} - e => return e, - } - let mut perms = stat.permissions(); - perms.set_readonly(false); - try!(fs::set_permissions(path, perms)); - filetime::set_file_times(path, newtime, newtime) - } - } - - fn c_exists(&self) -> bool { - fs::metadata(self).is_ok() - } - - fn c_is_file(&self) -> bool { - fs::metadata(self).map(|m| m.is_file()).unwrap_or(false) - } - - fn c_is_dir(&self) -> bool { - fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false) - } - - fn c_metadata(&self) -> io::Result { - fs::metadata(self) - } -} diff --git a/tests/test_cargo_test.rs b/tests/test.rs similarity index 99% rename from tests/test_cargo_test.rs rename to tests/test.rs index 953449db1..961498900 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test.rs @@ -1,9 +1,14 @@ +extern crate cargo; +extern crate cargotest; +extern crate hamcrest; + use std::fs::File; use std::io::prelude::*; use std::str; -use support::{project, execs, basic_bin_manifest, basic_lib_manifest}; -use support::paths::CargoPathExt; +use cargotest::{sleep_ms, is_nightly}; +use cargotest::support::{project, execs, basic_bin_manifest, basic_lib_manifest}; +use cargotest::support::paths::CargoPathExt; use hamcrest::{assert_that, existing_file, is_not}; use cargo::util::process; @@ -968,7 +973,7 @@ test foo ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured ")); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); assert_that(p.cargo("test"), execs().with_status(0) .with_stderr("\ @@ -1359,7 +1364,7 @@ fn build_then_selective_test() { .file("b/src/lib.rs", ""); assert_that(p.cargo_process("build"), execs().with_status(0)); - p.root().move_into_the_past().unwrap(); + p.root().move_into_the_past(); assert_that(p.cargo("test").arg("-p").arg("b"), execs().with_status(0)); } @@ -2010,7 +2015,7 @@ fn bin_does_not_rebuild_tests() { assert_that(p.cargo("test").arg("-v"), execs().with_status(0)); - ::sleep_ms(1000); + sleep_ms(1000); File::create(&p.root().join("src/main.rs")).unwrap() .write_all(b"fn main() { 3; }").unwrap(); @@ -2124,7 +2129,7 @@ test result: ok.[..] #[test] fn test_panic_abort_with_dep() { - if !::is_nightly() { + if !is_nightly() { return } let p = project("foo") diff --git a/tests/tests.rs b/tests/tests.rs deleted file mode 100644 index bad620d96..000000000 --- a/tests/tests.rs +++ /dev/null @@ -1,105 +0,0 @@ -#![deny(warnings)] - -extern crate bufstream; -extern crate cargo; -extern crate filetime; -extern crate flate2; -extern crate git2; -extern crate hamcrest; -extern crate libc; -extern crate rustc_serialize; -extern crate tar; -extern crate tempdir; -extern crate term; -extern crate url; -#[cfg(windows)] extern crate kernel32; -#[cfg(windows)] extern crate winapi; - -#[macro_use] -extern crate log; - -use cargo::util::Rustc; -use std::ffi::OsStr; -use std::time::Duration; - -mod support; - -mod test_bad_config; -mod test_bad_manifest_path; -mod test_cargo; -mod test_cargo_bench; -mod test_cargo_build_auth; -mod test_cargo_build_lib; -mod test_cargo_clean; -mod test_cargo_compile; -mod test_cargo_compile_custom_build; -mod test_cargo_compile_git_deps; -mod test_cargo_compile_path_deps; -mod test_cargo_compile_plugins; -mod test_cargo_compile_rustflags; -mod test_cargo_cross_compile; -mod test_cargo_doc; -mod test_cargo_features; -mod test_cargo_fetch; -mod test_cargo_freshness; -mod test_cargo_generate_lockfile; -mod test_cargo_init; -mod test_cargo_install; -mod test_cargo_metadata; -mod test_cargo_new; -mod test_cargo_package; -mod test_cargo_profiles; -mod test_cargo_overrides; -mod test_cargo_publish; -mod test_cargo_read_manifest; -mod test_cargo_registry; -mod test_cargo_run; -mod test_cargo_concurrent; -mod test_cargo_rustc; -mod test_cargo_rustdoc; -mod test_cargo_search; -mod test_cargo_test; -mod test_cargo_tool_paths; -mod test_cargo_config; -mod test_cargo_verify_project; -mod test_cargo_version; -mod test_shell; -mod test_cargo_death; -mod test_cargo_cfg; -mod test_cargo_net_config; - -thread_local!(static RUSTC: Rustc = Rustc::new("rustc").unwrap()); - -fn rustc_host() -> String { - RUSTC.with(|r| r.host.clone()) -} - -fn is_nightly() -> bool { - RUSTC.with(|r| { - r.verbose_version.contains("-nightly") || - r.verbose_version.contains("-dev") - }) -} - -fn process>(t: T) -> cargo::util::ProcessBuilder { - let mut p = cargo::util::process(t.as_ref()); - p.cwd(&support::paths::root()) - .env_remove("CARGO_HOME") - .env("HOME", support::paths::home()) - .env("CARGO_HOME", support::paths::home().join(".cargo")) - .env_remove("RUSTC") - .env_remove("RUSTFLAGS") - .env_remove("XDG_CONFIG_HOME") // see #2345 - .env("GIT_CONFIG_NOSYSTEM", "1") // keep trying to sandbox ourselves - .env_remove("CARGO_TARGET_DIR") // we assume 'target' - .env_remove("MSYSTEM"); // assume cmd.exe everywhere on windows - return p -} - -fn cargo_process() -> cargo::util::ProcessBuilder { - process(&support::cargo_dir().join("cargo")) -} - -fn sleep_ms(ms: u64) { - std::thread::sleep(Duration::from_millis(ms)); -} diff --git a/tests/test_cargo_tool_paths.rs b/tests/tool-paths.rs similarity index 94% rename from tests/test_cargo_tool_paths.rs rename to tests/tool-paths.rs index db76b45c2..2f61f503d 100644 --- a/tests/test_cargo_tool_paths.rs +++ b/tests/tool-paths.rs @@ -1,9 +1,13 @@ -use support::{path2url, project, execs}; +extern crate cargotest; +extern crate hamcrest; + +use cargotest::rustc_host; +use cargotest::support::{path2url, project, execs}; use hamcrest::assert_that; #[test] fn pathless_tools() { - let target = ::rustc_host(); + let target = rustc_host(); let foo = project("foo") .file("Cargo.toml", r#" @@ -31,7 +35,7 @@ fn pathless_tools() { #[test] fn absolute_tools() { - let target = ::rustc_host(); + let target = rustc_host(); // Escaped as they appear within a TOML config file let config = if cfg!(windows) { @@ -72,7 +76,7 @@ fn absolute_tools() { #[test] fn relative_tools() { - let target = ::rustc_host(); + let target = rustc_host(); // Escaped as they appear within a TOML config file let config = if cfg!(windows) { diff --git a/tests/test_cargo_verify_project.rs b/tests/verify-project.rs similarity index 92% rename from tests/test_cargo_verify_project.rs rename to tests/verify-project.rs index 64afb1d35..7e76ae04d 100644 --- a/tests/test_cargo_verify_project.rs +++ b/tests/verify-project.rs @@ -1,4 +1,7 @@ -use support::{project, execs, main_file, basic_bin_manifest}; +extern crate cargotest; +extern crate hamcrest; + +use cargotest::support::{project, execs, main_file, basic_bin_manifest}; use hamcrest::{assert_that}; fn verify_project_success_output() -> String { diff --git a/tests/test_cargo_version.rs b/tests/version.rs similarity index 90% rename from tests/test_cargo_version.rs rename to tests/version.rs index af54662c7..e8b38262a 100644 --- a/tests/test_cargo_version.rs +++ b/tests/version.rs @@ -1,6 +1,10 @@ -use support::{project, execs}; +extern crate cargo; +extern crate cargotest; +extern crate hamcrest; +extern crate rustc_serialize; + +use cargotest::support::{project, execs}; use hamcrest::assert_that; -use cargo; #[test] fn simple() { -- 2.30.2